package com.sprd.simple.fragment;

import android.app.ActivityManager;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.net.Uri;


import com.sprd.simple.adapter.FifthPageAdapter;
import com.sprd.simple.launcher.BrowserApplicationActivity;
import com.sprd.simple.launcher.Launcher;
import com.sprd.simple.launcher.R;
import com.sprd.simple.launcher.ToolsActivity;
import com.sprd.simple.model.IconInfo;
import com.sprd.simple.model.LauncherGridView;
import com.sprd.simple.model.TextSpeech;
import com.sprd.simple.util.AppIntentUtil;
import com.sprd.simple.util.MemoryInfoUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by SPRD on 2016/7/19.
 */
public class FifthWorkspaceFragment extends LauncherFragment implements
        AdapterView.OnItemClickListener{
    private static final String TAG = "FifthWorkspaceFragment";



    private static final int ICON_CALCULATOR = 0;
    private static final int ICON_FACEBOOK = 1;
    private static final int ICON_FILR_EXPLOER = 2;
    private static final int ICON_NOTE = 3;
    private static final int ICON_SOUND_RECORDER = 4;
    private static final int ICON_YOUTUBE = 5;
	

     private Context mContext;


    public FifthWorkspaceFragment() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_fifth_pager, container, false);

        mContext = getContext();
        appNameForTTS = new String[]{getResources().getString(R.string.calculator_name),
                getResources().getString(R.string.facebook_name), getResources().getString(R.string.file_manager_name),
                getResources().getString(R.string.note_name), getResources().getString(R.string.sound_recorder_name),
                getResources().getString(R.string.youtube_name)};

        // init GridView
        mGridView = (LauncherGridView) rootView.findViewById(R.id.fifth_grid_view);
        mAdapter = new FifthPageAdapter(getData(), getActivity(), appNameForTTS,mGridView);
        mGridView.setAdapter(mAdapter);
        mGridView.setSelector(getContext().getResources().getDrawable(R.color.transparent));
        // View click Listener
        mGridView.setOnItemClickListener(this);

        return rootView;
    }

	
    private ArrayList getData() {
        ArrayList<IconInfo> list = new ArrayList<IconInfo>();

        IconInfo calculator = new IconInfo();
        calculator.setIconName(getResources().getString(R.string.calculator_name));
        calculator.setIconImage(R.drawable.app_calculator);
        calculator.setPackageName(getResources().getString(R.string.calculator_package));
        list.add(calculator);
		
        IconInfo facebook = new IconInfo();
        facebook.setIconName(getResources().getString(R.string.facebook_name));
        facebook.setIconImage(R.drawable.app_facebook);
        facebook.setPackageName(getResources().getString(R.string.facebook_package));
        list.add(facebook);

        IconInfo file_manager = new IconInfo();
        file_manager.setIconName(getResources().getString(R.string.file_manager_name));
        file_manager.setIconImage(R.drawable.app_file_exploer);
        file_manager.setPackageName(getResources().getString(R.string.file_manager_package));
        list.add(file_manager);
		
        IconInfo note = new IconInfo();
        note.setIconName(getResources().getString(R.string.note_name));
        note.setIconImage(R.drawable.app_note);
        note.setPackageName(getResources().getString(R.string.note_package));
        list.add(note);

        IconInfo sound_recorder = new IconInfo();
        sound_recorder.setIconName(getResources().getString(R.string.sound_recorder_name));
        sound_recorder.setIconImage(R.drawable.app_sound_recoreder);
        sound_recorder.setPackageName(getResources().getString(R.string.sound_recorder_package));
        list.add(sound_recorder);

        IconInfo youtube = new IconInfo();
        youtube.setIconName(getResources().getString(R.string.youtube_name));
        youtube.setIconImage(R.drawable.app_youtube);
        youtube.setPackageName(getResources().getString(R.string.youtube_package));
        list.add(youtube);

        return list;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        switch (position) {
            case ICON_CALCULATOR:
                intent.setClassName(getResources().getString(R.string.calculator_package),
                        getResources().getString(R.string.calculator_activity));
                break;
            case ICON_FACEBOOK:
                intent.setClassName(getResources().getString(R.string.facebook_package),
                        getResources().getString(R.string.facebook_activity));
                break;
            case ICON_FILR_EXPLOER:
                intent.setClassName(getResources().getString(R.string.file_manager_package),
                        getResources().getString(R.string.file_manager_activity));
                break;
            case ICON_NOTE:
                intent.setClassName(getResources().getString(R.string.note_package),
                        getResources().getString(R.string.note_activity));
                break;
            case ICON_SOUND_RECORDER:
                intent.setClassName(getResources().getString(R.string.sound_recorder_package),
                        getResources().getString(R.string.sound_recorder_activity));
                break;
            case ICON_YOUTUBE:
                //intent.setClassName(getResources().getString(R.string.youtube_package),
                //        getResources().getString(R.string.youtube_activity));
				Uri uri = Uri.parse("http://www.youtube.com");
				Intent it = new Intent(Intent.ACTION_VIEW, uri);
				intent.setClassName(getResources().getString(R.string.browser_package),
                        getResources().getString(R.string.browser_activity));  
				startActivity(it);
                break;
            default:
                break;
        }

        try {
            AppIntentUtil.intentSetFlag(intent);
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.i(TAG, "App not found");
            Toast.makeText(getActivity(), R.string.app_not_found, Toast.LENGTH_LONG).show();
        }
    }

}

